package com.cndongtai.net.analysis;

import java.math.BigDecimal;
import java.util.Date;

import com.cndongtai.dao.impl.TorchnInverterCollectRecordDao;
import com.cndongtai.po.TorchnInverterCollectRecord;
import com.cndongtai.util.ByteUtil;
import com.cndongtai.util.CRC16;

public class WIFIMessageFilter {
	
	public static TorchnInverterCollectRecordDao torchnInverterCollectRecordDao;

	public static void doMsg(byte[] buf) {
		
		// 获取签到报文
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<buf.length;i++) {
			sb.append(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(buf[i])).toUpperCase())).append(" ");
		}
		
		System.out.println(new Date() + "WIFIMessageFilter WIFIreq===>" + sb.toString());
		//A2 5B 00 10 48 A1 00 00 00 01 11 11 00 00 82 B5
		
		// 判断报文头
		if(buf.length<10) {
			System.out.println("报文不合法1");
			return;
		}
		
		// 终端ID
		String id = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(buf[0])).toUpperCase());
		// 功能代码
		String code =  ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(buf[1])).toUpperCase());
		if(!"03".equals(code)&&!"04".equals(code)) {
			System.out.println("报文不合法2");
			return;
		}
		
		// 获取长度
		int msgsize = ByteUtil.getUnsignedByte(buf[2]);
		System.out.println(buf.length+"=="+msgsize);
		// 校验报文长度
		if(buf.length!=msgsize+5) {
			System.out.println("报文不合3");
			return;
		}
		
		// CRC16校验
		int crcReq = CRC16.calcCrc16(buf,0, 47);
		int crcvalReq = Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(buf[48])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(buf[47])).toUpperCase()), 16);
		if(crcReq!=crcvalReq) {
			System.out.println("CRC校验失败，" + crcReq + " " + crcvalReq);
			return;
		}
		
		// 报文类型
		TorchnInverterCollectRecord record = new TorchnInverterCollectRecord();
		
		record.setDeviceName("WIFI_ID_"+id);
		record.setInputVol(getDecimal10(buf,3,4));
		record.setOutputVol(getDecimal10(buf,5,6));
		record.setBatteryVol(getDecimal10(buf,7,8));
		
		record.setOutputCurrent(getDecimal10(buf,9,10));
		record.setOutputPercent(getDecimal1(buf,11,12));
		record.setOutputPower(getDecimal1(buf,13,14));
		record.setChargeCurrent(getDecimal1(buf,15,16));
		record.setBridgeMaxCurrent(getDecimal1(buf,17,18));
		record.setInputHz(getDecimal10(buf,19,20));
		record.setTemperature(getDecimal1(buf,21,22));
		record.setWorkState(getInteger(buf,23,24));
		record.setChargePercent(getDecimal1(buf,25,26));
		record.setOutputVolRegulation(getDecimal1(buf,27,28));
		record.setOutputMaxVol(getDecimal10(buf,29,30));
		
		record.setMpptBatteryVol(getDecimal10(buf,31,32));
		record.setPv2Vol(getDecimal10(buf,33,34));
		record.setPv1Current(getDecimal10(buf,35,36));
		record.setPv2Current(getDecimal10(buf,37,38));
		record.setPv1Vol(getDecimal10(buf,39,40));
		record.setPvPowerGeneration(getDecimal10(buf,41,42));
		record.setWorkCurrent(getDecimal10(buf,43,44));
		record.setChargeMaxCurrent(getDecimal10(buf,45,46));
		
		record.setCollectTime(new Date());
		
		torchnInverterCollectRecordDao.addTorchnInverterCollectRecord(record);
	}
	
	private static BigDecimal getDecimal10(byte[] buf, int indexHight, int indexLow) {
		return new BigDecimal(Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(buf[indexHight])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(buf[indexLow])).toUpperCase()), 16)/10.0);
	}
	
	private static BigDecimal getDecimal1(byte[] buf, int indexHight, int indexLow) {
		return new BigDecimal(Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(buf[indexHight])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(buf[indexLow])).toUpperCase()), 16));
	}
	
	private static Integer getInteger(byte[] buf, int indexHight, int indexLow) {
		return Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(buf[indexHight])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(buf[indexLow])).toUpperCase()), 16);
	}
}
